//9agw-499z-52v7
// q4i8-vks2-dfw8
package cn.sdjzu.exercise;

import java.io.IOException;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import cc.lotuscard.LotusCard;
import cn.sdjzu.exercise.db.DBHelper;

public class LoginActivity extends BasedActivity {
	ExerciseApp app;

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ExerciseApp.getInstance().addActivity(this);
		setContentView(R.layout.login);
		preferences = getSharedPreferences("itYao", Context.MODE_WORLD_READABLE);
		editor = preferences.edit();
		editor.putString("TransmissionWay", "selectWIFI");
		editor.commit();
		app = (ExerciseApp) LoginActivity.this.getApplication();
		mReception = new EditText(this); 
		// DBNoDataToast=Toast.makeText(this, "数据库错误", Toast.LENGTH_LONG);
		// ReadCardThread thread = new ReadCardThread();
		// thread.start();
		mHandler = new Handler() {
			public void handleMessage(android.os.Message msg) {
				// 卡号得到之后
				AlertDialog.Builder builder = new Builder(LoginActivity.this);
				builder.setTitle(R.string.notice);
				builder.setMessage(R.string.reg_alert_msg);
				builder.setPositiveButton(R.string.ok,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								Intent intent = new Intent();
								intent.setClass(LoginActivity.this,
										PersonalInfoActivity.class);
								startActivity(intent);
							}
						});
				builder.setNegativeButton(R.string.cancel,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
							}
						});
				builder.create().show();
			}
		};
	}

	Handler mHandler;

	class ReadCardThread extends Thread {
		LotusCard reading;
		boolean cardFlg;
		String cardNo;

		public ReadCardThread() {
			reading = new LotusCard(LoginActivity.this);
			cardNo = "";
			cardFlg = reading.initCardReading();
		}

		@Override
		public void run() {
			do {
				cardNo = reading.getCardNo();
				if (!"".equals(cardNo)) {
					app.currentCardNo = cardNo;
					DBHelper dbHelper = new DBHelper(LoginActivity.this);
					if (dbHelper.isExistCardNoInDB(DBHelper.TBL_USER, cardNo)) {
						editor.putString("userName", dbHelper.getDataFromDB(
								DBHelper.TBL_USER, DBHelper.TBL_USER_NAME,
								app.currentCardNo));
						editor.commit();
						Intent intent = new Intent();
						intent.setClass(LoginActivity.this,
								ControlPanelActivity.class);
						startActivity(intent);
					} else {
						mHandler.obtainMessage().sendToTarget();
					}
				}
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			} while ("".equals(cardNo) && cardFlg);
		}
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onBackPressed() {
		close();
	}

	public void NoCardTest(View v) {
		final EditText et = new EditText(this);
		et.setInputType(0x81);
		new AlertDialog.Builder(this)
				.setTitle("Password")
				.setIcon(android.R.drawable.ic_dialog_info)
				.setView(et)
				.setPositiveButton(R.string.ok,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// if ("654321".equals(et.getText().toString()))
								// {
								if (true) {
									try {
										TXTFile();
									} catch (IOException e) {
										e.printStackTrace();
									}
									app.currentCardNo = "000000";
									editor.putString("userName", "Admin");
									editor.commit();
									/*
									 * DBHelper dbHelper = new
									 * DBHelper(LoginActivity.this);
									 * ContentValues values = new
									 * ContentValues();
									 * values.put(DBHelper.TBL_USER_CARDNO,
									 * app.currentCardNo);
									 * dbHelper.insertData2DB
									 * (DBHelper.TBL_USER_EXT, values);
									 */
									Intent intent = new Intent();
									intent.setClass(LoginActivity.this,
											ControlPanelActivity.class);
									startActivity(intent);
									LoginActivity.this.finish();
								}
							}
						}).setNegativeButton(R.string.cancel, null).show();
	}

	EditText mReception;

	@SuppressWarnings("static-access")
	public void TXTFile() throws IOException {
		String filePath = LoginActivity.this.getCacheDir().getAbsolutePath();
		// String filePath = "/System/";
		TXT = new ReadWriteFile(filePath);
		TXT.creatTxtFile();
		TXT.writeTxtFile(filePath);
	}
}